import {Promise} from 'es6-promise';
import * as utils from "../core/utils";
import {Magics} from "../core/magics";
import {MongoColls} from "../config/mongo";
import {BaseModel} from "../core/base.model";
import {Collection, ObjectID} from "mongodb";
import {SexType, CursorType} from "../core/defines";
import {
    FindParam,
    CountParam,
    InsertParam,
    UpdateParam,
    DeleteParam,
    FindOneParam,
    AggregateParam,
} from "../core/params";

export interface UserInfo {
    _id?:ObjectID,
    name?:string,
    phone?:string,
    wechat?:string,
    prov?:{
        name:string,
        id:number
    },
    city?:{
        name:string,
        id:number
    },
    dist?:{
        name:string,
        id:number
    },
    addr?:string,
    sex?:SexType,
    password?:string,
    nick_name?:string,
    portrait?:string,
    time?: Date,
}

export enum LoginType {
    wechat = 1,
    phone = 2,
}

export interface UserFilter {
    _id?:ObjectID,
    wechat?:string,
    phone?:string,
}

export default class UserModel extends BaseModel {
    name() {
        return 'UserModel';
    }

    constructor(coll:Collection) {
        super(coll);
    }

    checkExist(filter:UserFilter):Promise<any> {
        return this.findOne(new FindOneParam(filter));
    }

    createUser(info:UserInfo):Promise<any> {
        info.time = new Date();
        return this.insertOne(new InsertParam<UserInfo>(info));
    }

}
